# coding: utf-8
"""
@python version : python3.10
@file name      : app05.py
@date           : 2024/5/15 14:46
@author         : ziyang.yang@aliyun.com
@gitee          : https://gitee.com/ziyangyang1318
@blog           : blog.yangziyang.top
@describe       : 
"""
from fastapi import FastAPI, File, UploadFile,APIRouter
from typing import List

app05 = APIRouter()

# file: bytes = File()：适合小文件上传
@app05.post("/files/")
async def create_file(file: bytes = File()):
    print("file:", file)
    return {"file_size": len(file)}


@app05.post("/multiFiles/")
async def create_files(files: List[bytes] = File()):
    return {"file_sizes": [len(file) for file in files]}


# file: UploadFile：适合大文件上传

@app05.post("/uploadFile/") # 单文件上传
async def create_upload_file(file: UploadFile):
    with open(f"{file.filename}", 'wb') as f:
        for chunk in iter(lambda: file.file.read(1024), b''):
            f.write(chunk)

    return {"filename": file.filename}


@app05.post("/multiUploadFiles/") # 多文件上传
async def create_upload_files(files: List[UploadFile]):
    return {"filenames": [file.filename for file in files]}